package ar.edu.utn.frgp.mvc.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ar.edu.utn.frgp.beans.ClienteBean;
import ar.edu.utn.frgp.beans.MessageBean;
import ar.edu.utn.frgp.banco.business.Implements.ImpExtDep;
import ar.edu.utn.frgp.banco.dao.Implements.ImpClientes;
import ar.edu.utn.frgp.banco.domain.Cliente;

/**
 * Servlet implementation class DepositosServlet
 */
public class DepositosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DepositosServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("accion").equals("Cargar Cuentas")) {
			cargar(request, response);
		}

		if (request.getParameter("accion").equals("Depositar")) {
			try {
				depositar(request, response);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void cargar(HttpServletRequest request, HttpServletResponse response) {

		Cliente cl = new Cliente();
		ClienteBean clientebean = new ClienteBean();
		List<Cliente> lista_cliente = new ArrayList<Cliente>();

		int ID_Cliente = Integer.parseInt(request.getSession()
				.getAttribute("ID_Cliente").toString());
		try {
			ImpClientes clientedao = new ImpClientes();
			cl = clientedao.ObtenerCliente(ID_Cliente);

			lista_cliente.add(cl);

			// guardo el objeto cliente en el objeto bean
			clientebean.setClientes(lista_cliente);

		} catch (Exception e) {
			MessageBean aux = new MessageBean();
			aux.setMensaje(e.getMessage());
			request.setAttribute("error", aux);
		}

		request.setAttribute("cliente", clientebean);
		try {
			request.getRequestDispatcher("Depositos.jsp").forward(request,
					response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// FUNCION DEPOSITAR
	private void depositar(HttpServletRequest request,
			HttpServletResponse response) throws ParseException {

		ImpExtDep deposito = new ImpExtDep();
		try {
			Integer id_cuenta = Integer.parseInt(request.getParameter("cuenta")
					.toString());
			double monto = Double.parseDouble((request.getParameter("monto")));

			deposito.Depositar(id_cuenta, monto);

		} catch (Exception e) {
			MessageBean aux = new MessageBean();
			aux.setMensaje(e.getMessage());
			request.setAttribute("error", aux);
		}
		try {
			// PREGUNTAR COMO HACER PARA DIRIGIR A OTRO DIRECTORIO
			request.getRequestDispatcher("../../Blanco.jsp").forward(request,
					response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
